package com.sskj.ffex.v.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.sskj.ffex.common.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.ffex.MyAppLication;
import com.sskj.ffex.R;
import com.sskj.ffex.common.App;
import com.sskj.ffex.common.Constants;
import com.sskj.ffex.common.adapter.BaseAdapter;
import com.sskj.ffex.common.adapter.ViewHolder;
import com.sskj.ffex.m.bean.AseetListBean;
import com.sskj.ffex.utils.TipUtil;
import com.sskj.ffex.v.activity.AssetManagePresenter;
import com.sskj.ffex.v.activity.CapitalDetailsActivity;
import com.sskj.ffex.v.activity.GoogleLoginActivity;
import com.sskj.ffex.v.activity.LoginActivity;
import com.sskj.ffex.v.activity.MainActivity;
import com.sskj.ffex.v.activity.RechargeActivity;
import com.sskj.ffex.v.activity.WithdrawActivity;
import com.sskj.ffex.v.base.BaseFragment;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;

/**
 * ProjectName：ioaex
 * DESC: (类描述)
 * Created by 李岩 on 2018/8/31 0031
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class AssetFragment extends BaseFragment<MainActivity, AssetManagePresenter> implements OnRefreshListener {
    @BindView(R.id.asset_manage_recyclerview)
    RecyclerView mAssetManageRecyclerview;
    @BindView(R.id.asset_manage_smartrefresh)
    SmartRefreshLayout mAssetManageSmartrefresh;
    private BaseAdapter<AseetListBean.AseetBean> mAdapter;
    DecimalFormat mFormat = new DecimalFormat("0.0000");
    private View headerView;

    public static AssetFragment newInstance() {
        Bundle args = new Bundle();
        AssetFragment fragment = new AssetFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_asset;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MyAppLication.isLogin()) {
            mPresenter.requestUserAssetList();
        } else {
            // 弹出提示框
            if (!isHidden()) {
                mAdapter.setNewData(new ArrayList<AseetListBean.AseetBean>());
                mAdapter.removeAllHeaderView();
                mAdapter.setEmtyView();

                TipUtil.getTip(mActivity, App.INSTANCE.getString(R.string.strAssetFragment201), App.INSTANCE.getString(R.string.strAssetFragment202), App.INSTANCE.getString(R.string.strAssetFragment203), () -> {
                    LoginActivity.start(getContext(), 3);
                }).show();
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (MyAppLication.isLogin()) {
            mPresenter.requestUserAssetList();
        } else {
            if (!hidden) {
                // 弹出提示框
                mAdapter.setNewData(new ArrayList<AseetListBean.AseetBean>());
                mAdapter.removeAllHeaderView();
                mAdapter.setEmtyView();

                TipUtil.getTip(mActivity, App.INSTANCE.getString(R.string.strAssetFragment201), App.INSTANCE.getString(R.string.strAssetFragment202), App.INSTANCE.getString(R.string.strAssetFragment203), () -> {
                    LoginActivity.start(getContext(), 3);
                }).show();
            }


        }

    }

    @Override
    public AssetManagePresenter getPresenter() {
        return new AssetManagePresenter();
    }

    @Override
    public void initView() {
        ClassicsHeader classicsHeader = new ClassicsHeader(mActivity);
        classicsHeader.setBackgroundColor(getResources().getColor(R.color.background));
        mAssetManageSmartrefresh.setRefreshHeader(classicsHeader);
        mAssetManageSmartrefresh.setOnRefreshListener(this);

        mAssetManageRecyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        mAssetManageRecyclerview.addItemDecoration(new DividerItemDecoration(mActivity, LinearLayoutManager.VERTICAL));

        mAdapter = new BaseAdapter<AseetListBean.AseetBean>(R.layout.item_asset_manage, null, mAssetManageRecyclerview) {

            @Override
            public void bind(ViewHolder holder, AseetListBean.AseetBean item) {
                holder.setText(R.id.item_asset_manage_type, item.pname);
                holder.setText(R.id.item_asset_manage_available_balance, mFormat.format(item.usable));
                holder.setText(R.id.item_asset_manage_frozen_capital, mFormat.format(item.frost));
                holder.setOnClickListener(R.id.item_asset_manage_recharge, v -> {//充值
                    startActivity(new Intent(mActivity, RechargeActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.pid).putExtra(Constants.INTENT_ASSET_NAME, item.pname));
                });
                holder.setOnClickListener(R.id.item_asset_manage_withdraw, v -> {//提币
                    if (checkIdent(getActivity())) {
                        if (checkIdentHign(getActivity())) {



                            if (0 == MyAppLication.getGOOGLETYPE()) {
                                startActivity(new Intent(mActivity, WithdrawActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.pid).putExtra(Constants.INTENT_ASSET_NAME, item.pname));
                            } else {
                                GoogleLoginActivity.start(mActivity, 1, item);
                            }
                        }
                    }
                });
                holder.setOnClickListener(R.id.item_asset_manage_record, v -> {//账户明细
                    startActivity(new Intent(mActivity, CapitalDetailsActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.pid));
                });
            }
        };
        headerView = LayoutInflater.from(mActivity).inflate(R.layout.layout_asset_header, null);
        mAdapter.addHeaderView(headerView);

    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        mAssetManageSmartrefresh.finishRefresh();
        if (MyAppLication.isLogin()) {
            mPresenter.requestUserAssetList();
        } else {
            // 弹出提示框
            mAdapter.setNewData(new ArrayList<AseetListBean.AseetBean>());
            mAdapter.removeAllHeaderView();
            mAdapter.setEmtyView();
            TipUtil.getTip(mActivity, App.INSTANCE.getString(R.string.strAssetFragment201), App.INSTANCE.getString(R.string.strAssetFragment202), App.INSTANCE.getString(R.string.strAssetFragment203), () -> {
                LoginActivity.start(getContext(), 3);
            }).show();
        }
    }


    public void onDataSuccess(AseetListBean data) {
        if (MyAppLication.isLogin()) {
            if (headerView != null && mAdapter.getHeaderLayoutCount() == 0) {
                mAdapter.addHeaderView(headerView);
            }
            mAdapter.setNewData(data.asset);
        } else {
            mAdapter.removeAllHeaderView();
            mAdapter.setNewData(new ArrayList<AseetListBean.AseetBean>());
            mAdapter.setEmtyView();
            TipUtil.getTip(mActivity, App.INSTANCE.getString(R.string.strAssetFragment201), App.INSTANCE.getString(R.string.strAssetFragment202), App.INSTANCE.getString(R.string.strAssetFragment203), () -> {
                LoginActivity.start(getContext(), 3);
            }).show();
        }
    }
}
